package com.lagou.rpc.factory;

import com.lagou.rpc.api.RpcRegistryCenter;
import com.lagou.rpc.api.impl.ZookeeperRegistryCenter;
import com.lagou.rpc.config.ZookeeperConfig;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

/**
 * @param
 * @author oyz
 * @version 1.0
 * @description: TODO 注册工厂类
 * @date 2022/2/28 13:24
 */
public class RpcRegistryCenterFactory implements FactoryBean<RpcRegistryCenter> , DisposableBean {


    private RpcRegistryCenter rpcRegistryCenter;


    /**
     * @description: TODO 自定义bean对象,放入容器
     * @author oyz
     * @param
     * @date 2022/2/28 14:03
     * @version 1.0
     */
    @Override
    public RpcRegistryCenter getObject() {
        if(null == rpcRegistryCenter){
            /**
             * 调用他的构造方法，完成注册节点，上报连接时间的操作
             */
            rpcRegistryCenter = new ZookeeperRegistryCenter(ZookeeperConfig.getInstance().getZookeeperAddress());

        }
        return rpcRegistryCenter;
    }

    @Override
    public Class<?> getObjectType() {
        return RpcRegistryCenter.class;
    }


    @Override
    public void destroy() throws Exception {
        if (null != rpcRegistryCenter){
            rpcRegistryCenter.destroy();
        }
    }
}
